package com.example.mylibrarybase.yumengmeng02.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.bar.initAction_Bar;
import com.example.mylibrarybase.base.baseui.BaseActivity;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.yumengmeng02.adapter.UploadPhotoAdapter_02;
import com.example.mylibrarybase.yumengmeng02.bean.UploadFile_02;
import com.example.mylibrarybase.yumengmeng02.photopicker.PhotoPickerActivity;
import com.example.mylibrarybase.yumengmeng02.view.MyRecyclerView_02;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Activity_MyRecyclerView_02 extends BaseActivity {

    private static final int REQUEST_CODE_PHOTO = 1000;
    private static final int REQUEST_CODE_TAG = 2000;
    private static final int REQUEST_CODE_LOGIN = 3000;

    private static final int MSG_AUDIO_PREPARED = 4;
    private static final int MSG_VOICE_CHANGED = 5;
    private static final int MSG_DIALOG_DIMISS = 6;
    List<UploadFile_02> uploadFiles = new ArrayList<>();
    private MyRecyclerView_02 mActivity_02_recyclerview;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 100:
                    //      选取图片的 界面
                    Intent intent = new Intent(Activity_MyRecyclerView_02.this,PhotoPickerActivity.class );
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.putExtra(PhotoPickerActivity.EXTRA_SELECT_MODE, PhotoPickerActivity.MODE_MULTI);
                    startActivityForResult(intent, REQUEST_CODE_PHOTO);
                    break;
            }

        }
    };
    private UploadPhotoAdapter_02 mUploadPhotoAdapter;
    private initAction_Bar mBase_action_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public int getlayouXML() {
        return R.layout.activity_my_recycler_view_02;
    }



    @Override
    public void initView() {
        mBase_action_bar = (initAction_Bar) findViewById(R.id.base_action_bar);
        mBase_action_bar
                .setMode(initAction_Bar.AUTO_ONCLICK_MODE)
                .setCallBack(new initAction_Bar.Action_bar_call_back() {
                    @Override
                    public void getAction_barView_backbutton(TextView textView) {
                        textView.setText("返回");
                    }

                    @Override
                    public void getAction_barView_backbutton_icon(ImageView imageView) {

                    }

                    @Override
                    public void getAction_barView_title(TextView textView) {
                        textView.setText("图片轮播放大缩小");
                    }

                    @Override
                    public void getAction_barView_right_icon(ImageView imageView) {

                    }
                });

        mActivity_02_recyclerview = (MyRecyclerView_02) findViewById(R.id.activity_02_recyclerview);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mActivity_02_recyclerview.setLayoutManager(mLayoutManager);

        mUploadPhotoAdapter = new UploadPhotoAdapter_02(this, handler);
        mActivity_02_recyclerview.setAdapter(mUploadPhotoAdapter);
        initListener();
    }

    private void initListener() {
        mUploadPhotoAdapter.setOnItemClickListener(new UploadPhotoAdapter_02.MyItemClickListener() {
            @Override
            public void onItemClick(View view, List<UploadFile_02> uploadFiles, int postion) {
                LogUtils.i("现在点的是集合里面的postion"+postion+"内容是"+uploadFiles.get(postion).getFile_path());
                Intent intent = new Intent(Activity_MyRecyclerView_02.this, photoView_02.class);
                Bundle bundle = new Bundle();
                bundle.putInt("postion",postion);
                bundle.putSerializable("list",(Serializable)uploadFiles);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case RESULT_OK:
                uploadFiles.clear();
                ArrayList<String> listExtra = data.getStringArrayListExtra(PhotoPickerActivity.KEY_RESULT);
                if (listExtra.size()>0&&listExtra!=null){
                    for (int i = 0; i < listExtra.size(); i++) {
                        UploadFile_02 uploadFile = new UploadFile_02();
                        uploadFile.setFile_path(listExtra.get(i));
                        uploadFiles.add(uploadFile);
                    }
                    mUploadPhotoAdapter.setData(uploadFiles);
                }

                break;
        }
    }
}
